/**********************************************************************************
***
***    Screencasting Teaching System C++ Library
***    Copyright (C) 2011  Jiri Novak <jiri.novak@petriny.net>
***                        Wadi Jalil Maluf <wadijm@gmail.com>
***
***    This file is part of the Screencasting Teaching System C++ Library.
***
***    This library is free software; you can redistribute it and/or modify
***    it under the terms of the GNU General Public License as published by
***   the Free Software Foundation; either version 3 of the License, or
***    (at your option) any later version.
***
***    This library is distributed in the hope that it will be useful,
***    but WITHOUT ANY WARRANTY; without even the implied warranty of
***    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
***   GNU General Public License for more details.
***
***    You should have received a copy of the GNU General Public License
***    along with this library; if not, write to the Free Software
***    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
***
*********************************************************************************/

#ifndef SCREENCASTING_TEACHING_SYSTEM___COMMON___XMLPROTOCOL_H_
#define SCREENCASTING_TEACHING_SYSTEM___COMMON___XMLPROTOCOL_H_

#include "model/user.h"
#include "model/course.h"
#include "model/chatmessage.h"
#include <libxml++/libxml++.h>
#include <log4cxx/logger.h>
#include <string>
#include <ctime>
#include <list>

/**
 *
 */
class XmlUtil {
public:
	XmlUtil();
	virtual ~XmlUtil();

	// loading
	void loadShows(std::istream &coursesFile, std::list<Course*> &courses, std::list<Lesson*> &lessons);
	void loadUsers(std::istream &usersFile, std::list<User*> & users);

	// parsing
	User* parseUser(const std::string &xmlStr);
	ChatMessage* parseChatMessage(const std::string &xmlStr);
	Lesson* parseLesson(const std::string &data);
	Course* parseCourse(const std::string &course);
	void parseShows(const std::string &xmlStr, std::list<Course*> &courses, std::list<Lesson*> &lessons);

	// serializing
	std::string serializeUser(User &user);
	std::string serializeChatMessage(ChatMessage &msg);
	std::string serializeLesson(Lesson &lesson);
	std::string serializeCourse(Course &course);
	std::string serializeShows(std::list<Course*> &courses, std::list<Lesson*> &lessons);

private:
	log4cxx::LoggerPtr logger_;

	// auxiliary parsing functions
	User* parseUserNode(xmlpp::Node *userNode);
	ChatMessage* parseChatMessageNode(xmlpp::Node *chatMessageNode);
	Lesson* parseLessonNode(xmlpp::Node *lessonNode);
	void parseLessonsNode(xmlpp::Node *lessonsNode, std::list<Lesson*> &lessonList);
	Course* parseCourseNode(xmlpp::Node *courseNode);
	void parseCoursesNode(xmlpp::Node *coursesNode, std::list<Course*> &list);

	// auxiliary functions (populating nodes)
	void populateUserNode(xmlpp::Node *userNode, const User &user);
	void populateLessonNode(xmlpp::Node *lessonNode, Lesson &lesson);
	void populateLessonsNode(xmlpp::Node *lessonsNode, const std::list<Lesson*> &list);
	void populateCourseNode(xmlpp::Node *courseNode, Course &course);
	void populateCoursesNode(xmlpp::Node *coursesNode, const std::list<Course*> &list);

	// time utils
	time_t getTime(const std::string &strData);
	std::string getStrTime(time_t time);
	std::string getStringFromInt(int number);
};

#endif /* SCREENCASTING_TEACHING_SYSTEM___COMMON_XMLPROTOCOL_H_ */
